/****************************************************************************************************************************************************************
 *																																								*
 *																																								*
 *																																								*
 *																																								*
 *						_____  ___________                   ___    ______      ______           _____     _______________________ 								*
 *						__  / / /__  /_  /_____________ _    __ |  / /__(_)________  /_____________  /_    ___    |_____/_/__  __ \								*
 *						_  / / /__  /_  __/_  ___/  __ `/    __ | / /__  /_  __ \_  /_  _ \_  __ \  __/    __  /| |___/_/ __  / / /								*
 *						/ /_/ / _  / / /_ _  /   / /_/ /     __ |/ / _  / / /_/ /  / /  __/  / / / /_      _  ___ |_/_/   _  /_/ / 								*
 *						\____/  /_/  \__/ /_/    \__,_/      _____/  /_/  \____//_/  \___//_/ /_/\__/      /_/  |_/_/     /_____/  								*
 *                                                                                                          													*
 *																																								*
 *																																								*
 *																																								*
 *																																								*
 *		@Project: Ultra Violent - Attack and Defend																												*
 *																																								*
 *																																								*
 *		@Author: Daniel "thiaZ" Koenen																															*
 *			@Used content																																		*
 *				@sscanf Plugin, foreach, Whirlpool Plugin, YSI - Y_Less																							*
 *				@zcmd - Zeex																																	*
 *				@Streamer Plugin, IRC Plugin - Incognito																										*
 *				@dini - Dracoblue																																*
 *				@MySQL Plugin - G-sTyLeZzZ																														*
 *			@Special thanks	to																																	*
 *				@Slice																																			*
 *																																								*
 *																																								*
 *		@File: uvaad.pwn - Main File																															*
 *																																								*
 *																																								*
 *		@This file is not released under any open source licence, you are not allowed to own this file except you have the author's permission.					*
 *																																								*
 * 																																								*
 *		@copyright (c) Daniel "thiaZ" Koenen 2010 - 2011 - danielkoenen@t-online.de																				*
 *																																								*
 ****************************************************************************************************************************************************************/			
 
enum E_BASE_CREATION
{
	iCP_Att,
	iCP_Def,
	iCP_CP,
	iMapIcon_Att,
	iMapIcon_Def,
	iMapIcon_CP,
	Text3D:attSpawn,
	Text3D:defSpawn,
	Text3D:cpSpawn
}

new baseCData[MAX_BASES][E_BASE_CREATION];

Hook:CCreation_OnRconCommand(cmd[])
{
    if(!strcmp(cmd, "showbases",true))
    {
		CCreation::showBases();
		print("Loaded bases.");
	}
	
	return true;
}
 
Hook:CCreation_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) 
{
	switch(dialogid)
	{
		case DIALOG_CHOOSE:
		{
			if(!response)
			{
				return true;
			}
			
			if(!isNumeric(inputtext))
			{
				CCreation::showBaseDialog(playerid, DIALOG_CHOOSE);
				SendClientMessage(playerid, C_YELLOW, "* Error: Invalid base.");
				return true;
			}
			
			new iBase = strval(inputtext);
			
			if(iBase > MAX_BASES || iBase < 0 || baseData[iBase][bIsValid] == false)
			{
				CCreation::showBaseDialog(playerid, DIALOG_CHOOSE);
				SendClientMessage(playerid, C_YELLOW, "* Error: Invalid base.");
				return true;
			}
			
			playerData[playerid][baseSelected] = strval(inputtext);
			CCreation::showBaseDialog(playerid, DIALOG_MAIN);
		}
		
		case DIALOG_MAIN:
		{
			if(!response)
			{
				CCreation::showBaseDialog(playerid, DIALOG_CHOOSE);
				return true;
			}
			
			switch(listitem)
			{
				case 0:
				{
					// Attacker Spawn
					new Float:X,
						Float:Y,
						Float:Z,
						Float:A,
						sPath[64],
						sTemp[128],
						INI:baseFile;
					
					GetPlayerPos(playerid, X, Y, Z);
					GetPlayerFacingAngle(playerid, A);
					
					format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
					format(sTemp, 128, "%.2f,%.2f,%.2f,%.2f", X, Y, Z, A);
					
					baseFile = INI_Open(sPath);
					INI_WriteString(baseFile, "att_spawn", sTemp);
					INI_Close(baseFile);
					
					format(sTemp, 128, "* Changed Attacker Spawn of base %d to %s.", playerData[playerid][baseSelected], sTemp);
					SendClientMessageToAll(C_YELLOW, sTemp);
					
					CCreation::destroyBases(playerData[playerid][baseSelected]);
					CCreation::reloadBases(playerData[playerid][baseSelected]);
					CCreation::showBases(playerData[playerid][baseSelected]);
					
					playerData[playerid][baseSelected] = -1;
				}
				
				case 1:
				{
					// Defender Spawn
					new Float:X,
						Float:Y,
						Float:Z,
						Float:A,
						sPath[64],
						sTemp[128],
						INI:baseFile;
					
					GetPlayerPos(playerid, X, Y, Z);
					GetPlayerFacingAngle(playerid, A);
					
					format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
					format(sTemp, 128, "%.2f,%.2f,%.2f,%.2f", X, Y, Z, A);
					
					baseFile = INI_Open(sPath);
					INI_WriteString(baseFile, "def_spawn", sTemp);
					INI_Close(baseFile);
					
					format(sTemp, 128, "* Changed Defender Spawn of base %d to %s.", playerData[playerid][baseSelected], sTemp);
					SendClientMessageToAll(C_YELLOW, sTemp);
					
					CCreation::destroyBases(playerData[playerid][baseSelected]);
					CCreation::reloadBases(playerData[playerid][baseSelected]);
					CCreation::showBases(playerData[playerid][baseSelected]);
					
					playerData[playerid][baseSelected] = -1;
				}
				
				case 2:
				{
					// Checkpoint Spawn
					new Float:X,
						Float:Y,
						Float:Z,
						sPath[64],
						sTemp[128],
						INI:baseFile;
					
					GetPlayerPos(playerid, X, Y, Z);
					
					format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
					format(sTemp, 128, "%.2f,%.2f,%.2f", X, Y, Z);
					
					baseFile = INI_Open(sPath);
					INI_WriteString(baseFile, "cp_spawn", sTemp);
					INI_Close(baseFile);
					
					format(sTemp, 128, "* Changed Checkpoint Spawn of base %d to %s.", playerData[playerid][baseSelected], sTemp);
					SendClientMessageToAll(C_YELLOW, sTemp);
					
					CCreation::destroyBases(playerData[playerid][baseSelected]);
					CCreation::reloadBases(playerData[playerid][baseSelected]);
					CCreation::showBases(playerData[playerid][baseSelected]);
					
					playerData[playerid][baseSelected] = -1;
				}
				
				case 3:
				{
					// Cam Spawn
					new Float:X,
						Float:Y,
						Float:Z,
						sPath[64],
						sTemp[128],
						INI:baseFile;
					
					GetPlayerPos(playerid, X, Y, Z);
					
					format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
					format(sTemp, 128, "%.2f,%.2f,%.2f", X, Y, Z);
					
					baseFile = INI_Open(sPath);
					INI_WriteString(baseFile, "cam_spawn", sTemp);
					INI_Close(baseFile);
					
					format(sTemp, 128, "* Changed Camera Spawn of base %d to %s.", playerData[playerid][baseSelected], sTemp);
					SendClientMessageToAll(C_YELLOW, sTemp);
					
					CCreation::destroyBases(playerData[playerid][baseSelected]);
					CCreation::reloadBases(playerData[playerid][baseSelected]);
					CCreation::showBases(playerData[playerid][baseSelected]);
					
					playerData[playerid][baseSelected] = -1;
				}
				
				case 5:
				{
					// Cam Spawn
					new xInterior,
						sPath[64],
						sTemp[128],
						INI:baseFile;
					
					xInterior = GetPlayerInterior(playerid);
					
					format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
					
					baseFile = INI_Open(sPath);
					INI_WriteInt(baseFile, "interior", xInterior);
					INI_Close(baseFile);
					
					format(sTemp, 128, "* Changed Interior of base %d to %d.", playerData[playerid][baseSelected], xInterior);
					SendClientMessageToAll(C_YELLOW, sTemp);
					
					CCreation::destroyBases(playerData[playerid][baseSelected]);
					CCreation::reloadBases(playerData[playerid][baseSelected]);
					CCreation::showBases(playerData[playerid][baseSelected]);
					
					playerData[playerid][baseSelected] = -1;
				}
				
				case 4: CCreation::showBaseDialog(playerid, DIALOG_DESCR);
			}
		}
		
		case DIALOG_DESCR:
		{
			if(!response)
			{
				CCreation::showBaseDialog(playerid, DIALOG_MAIN);
				return true;
			}
			
			if(!strlen(inputtext))
			{
				CCreation::showBaseDialog(playerid, DIALOG_DESCR);
				SendClientMessage(playerid, C_YELLOW, "* Error: Invalid input.");
				return true;
			}
			
			new sPath[64],
				sTemp[128];
				
			format(sPath, 64, "/Uv_AAD/Bases/%d.ini", playerData[playerid][baseSelected]);
			
			new INI:baseFile = INI_Open(sPath);
			INI_WriteString(baseFile, "description", inputtext);
			INI_Close(baseFile);
			
			format(sTemp, 128, "* Changed Description of base %d to %s.", playerData[playerid][baseSelected], inputtext);
			SendClientMessageToAll(C_YELLOW, sTemp);
					
			CCreation::destroyBases(playerData[playerid][baseSelected]);
			CCreation::reloadBases(playerData[playerid][baseSelected]);
			CCreation::showBases(playerData[playerid][baseSelected]);
					
			playerData[playerid][baseSelected] = -1;
		}
		
		case DIALOG_CREATE:
		{
			if(!response)
			{
				return true;
			}
			
			if(!strlen(inputtext))
			{
				CCreation::showBaseDialog(playerid, DIALOG_CREATE);
				SendClientMessage(playerid, C_YELLOW, "* Error: Invalid input.");
				return true;
			}
			
			new iBaseID;
			for(new i; i < MAX_BASES; i++)
			{
				if(baseData[i][bIsValid] == false)
				{
					break;
				}
				
				iBaseID = (i + 1);
			}
			
			new INI:baseFile = INI_Open(CBase::getFile(iBaseID));
			INI_WriteString(baseFile, "description", inputtext);
			INI_Close(baseFile);
			
			CCreation::reloadBases(iBaseID);
			SendFormattedMessageToAll(C_YELLOW, "Base %d (%s) has been created, use /base to edit it's values.", iBaseID, inputtext);
		}
	}
	
	return true;
}

stock CCreation::showBases(iExtra = -1)
{
	new sTemp[128];
	if(iExtra == -1)
	{
		print("Loading bases...");
		for(new i; i < MAX_BASES; i++)
		{
			if(!baseData[i][bIsValid])
			{
				break;
			}
			
			print("Loading CPs...");
			baseCData[i][iCP_Att] = CreateDynamicCP(baseData[i][a_fX], baseData[i][a_fY], baseData[i][a_fZ], 2.0);
			baseCData[i][iCP_Def] = CreateDynamicCP(baseData[i][d_fX], baseData[i][d_fY], baseData[i][d_fZ], 2.0);
			baseCData[i][iCP_CP] = CreateDynamicCP(baseData[i][cp_fX], baseData[i][cp_fY], baseData[i][cp_fZ], 2.0);
			print("Done.");
			
			print("Loading Map Icons...");
			baseCData[i][iMapIcon_Att] = CreateDynamicMapIcon(baseData[i][a_fX], baseData[i][a_fY], baseData[i][a_fZ], 32, 0xFFFFFFFF);
			baseCData[i][iMapIcon_Def] = CreateDynamicMapIcon(baseData[i][d_fX], baseData[i][d_fY], baseData[i][d_fZ], 31, 0xFFFFFFFF);
			baseCData[i][iMapIcon_CP] = CreateDynamicMapIcon(baseData[i][cp_fX], baseData[i][cp_fY], baseData[i][cp_fZ], 12, 0xFFFFFFFF);
			print("Done.");
		
			print("Loading  3D Texts...");
			format(sTemp, 128, "Base: %d | Description: %s\nAttacker Spawn", i, baseData[i][sDescription]);
			baseCData[i][attSpawn] = CreateDynamic3DTextLabel(sTemp, C_ATT, baseData[i][a_fX], baseData[i][a_fY], baseData[i][a_fZ] + 0.5, 50.0);
			
			format(sTemp, 128, "Base: %d | Description: %s\nDefender Spawn", i, baseData[i][sDescription]);
			baseCData[i][defSpawn] = CreateDynamic3DTextLabel(sTemp, C_DEF, baseData[i][d_fX], baseData[i][d_fY], baseData[i][d_fZ] + 0.5, 50.0);
				
			format(sTemp, 128, "Base: %d | Description: %s\nCheckpoint", i, baseData[i][sDescription]);
			baseCData[i][cpSpawn] = CreateDynamic3DTextLabel(sTemp, C_YELLOW, baseData[i][cp_fX], baseData[i][cp_fY], baseData[i][cp_fZ] + 0.5, 50.0);
			print("Done.");
		}
	}
	
	else
	{
		baseCData[iExtra][iCP_Att] = CreateDynamicCP(baseData[iExtra][a_fX], baseData[iExtra][a_fY], baseData[iExtra][a_fZ], 2.0);
		baseCData[iExtra][iCP_Def] = CreateDynamicCP(baseData[iExtra][d_fX], baseData[iExtra][d_fY], baseData[iExtra][d_fZ], 2.0);
		baseCData[iExtra][iCP_CP] = CreateDynamicCP(baseData[iExtra][cp_fX], baseData[iExtra][cp_fY], baseData[iExtra][cp_fZ], 2.0);
			
		baseCData[iExtra][iMapIcon_Att] = CreateDynamicMapIcon(baseData[iExtra][a_fX], baseData[iExtra][a_fY], baseData[iExtra][a_fZ], 32, 0xFFFFFFFF);
		baseCData[iExtra][iMapIcon_Def] = CreateDynamicMapIcon(baseData[iExtra][d_fX], baseData[iExtra][d_fY], baseData[iExtra][d_fZ], 31, 0xFFFFFFFF);
		baseCData[iExtra][iMapIcon_CP] = CreateDynamicMapIcon(baseData[iExtra][cp_fX], baseData[iExtra][cp_fY], baseData[iExtra][cp_fZ], 12, 0xFFFFFFFF);
			
		format(sTemp, 128, "Base: %d | Description: %s\nAttacker Spawn", iExtra, baseData[iExtra][sDescription]);
		baseCData[iExtra][attSpawn] = CreateDynamic3DTextLabel(sTemp, C_ATT, baseData[iExtra][a_fX], baseData[iExtra][a_fY], baseData[iExtra][a_fZ] + 0.5, 50.0);
		
		format(sTemp, 128, "Base: %d | Description: %s\nDefender Spawn", iExtra, baseData[iExtra][sDescription]);
		baseCData[iExtra][defSpawn] = CreateDynamic3DTextLabel(sTemp, C_DEF, baseData[iExtra][d_fX], baseData[iExtra][d_fY], baseData[iExtra][d_fZ] + 0.5, 50.0);
			
		format(sTemp, 128, "Base: %d | Description: %s\nCheckpoint", iExtra, baseData[iExtra][sDescription]);
		baseCData[iExtra][cpSpawn] = CreateDynamic3DTextLabel(sTemp, C_YELLOW, baseData[iExtra][cp_fX], baseData[iExtra][cp_fY], baseData[iExtra][cp_fZ] + 0.5, 50.0);
	}
	
	printf("CPs: %d", CountDynamicCPs());
	printf("Map icons: %d", CountDynamicMapIcons());
	printf("Text labels: %d", CountDynamic3DTextLabels());
	return;
}


stock CCreation::destroyBases(iExtra = -1)
{
	if(iExtra == -1)
	{
		for(new i; i < MAX_BASES; i++)
		{
			if(!baseData[i][bIsValid])
			{
				continue;
			}
			
			DestroyDynamicCP(baseCData[i][iCP_Att]);
			DestroyDynamicCP(baseCData[i][iCP_Def]);
			DestroyDynamicCP(baseCData[i][iCP_CP]);
			
			DestroyDynamicMapIcon(baseCData[i][iMapIcon_Att]);
			DestroyDynamicMapIcon(baseCData[i][iMapIcon_Def]);
			DestroyDynamicMapIcon(baseCData[i][iMapIcon_CP]);
			
			DestroyDynamic3DTextLabel(baseCData[i][attSpawn]);
			DestroyDynamic3DTextLabel(baseCData[i][defSpawn]);
			DestroyDynamic3DTextLabel(baseCData[i][cpSpawn]);
		}
	}
	
	else
	{
		DestroyDynamicCP(baseCData[iExtra][iCP_Att]);
		DestroyDynamicCP(baseCData[iExtra][iCP_Def]);
		DestroyDynamicCP(baseCData[iExtra][iCP_CP]);
			
		DestroyDynamicMapIcon(baseCData[iExtra][iMapIcon_Att]);
		DestroyDynamicMapIcon(baseCData[iExtra][iMapIcon_Def]);
		DestroyDynamicMapIcon(baseCData[iExtra][iMapIcon_CP]);
			
		DestroyDynamic3DTextLabel(baseCData[iExtra][attSpawn]);
		DestroyDynamic3DTextLabel(baseCData[iExtra][defSpawn]);
		DestroyDynamic3DTextLabel(baseCData[iExtra][cpSpawn]);
	}
	
	return;
}

stock CCreation::showBaseDialog(playerid, iDialog)
{
	switch(iDialog)
	{
		case DIALOG_CHOOSE: ShowPlayerDialog(playerid, DIALOG_CHOOSE, DIALOG_STYLE_INPUT, "Base Creation", "Enter a base to edit.", "Continue", "Close");
		case DIALOG_MAIN: ShowPlayerDialog(playerid, DIALOG_MAIN, DIALOG_STYLE_LIST, "Base Creation", "Attacker Spawn\nDefender Spawn\nCheckpoint Spawn\nCamera Spawn\nDescription\nInterior", "Continue", "Back"); 
		case DIALOG_DESCR: ShowPlayerDialog(playerid, DIALOG_DESCR, DIALOG_STYLE_INPUT, "Base Creation", "Enter the base description\n and press Update.", "Update", "Close");
		case DIALOG_CREATE: ShowPlayerDialog(playerid, DIALOG_CREATE, DIALOG_STYLE_INPUT, "Base Creation", "Enter the description for your base.", "Continue", "Close");
	}
	
	return;
}

stock CCreation::reloadBases(iBaseID)
{
	INI_ParseFile(CBase::getFile(iBaseID), "onBaseLoad", false, true, iBaseID, true, false);
	return true;
}

